package org.sippr.controller;

import org.sippr.pojo.Project;
import org.sippr.pojo.Result;
import org.sippr.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/project")

public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @GetMapping("/list")
    public Result list() {
        List<Project> projectList = projectService.list();
        for (Project project : projectList) {
            project.setImgUrl("/file/project/project.jpg");
        }
        return Result.success(projectList);
    }

    @PostMapping("/add")
    public Result add(@RequestBody @Validated(Project.Add.class) Project value) {
        Project project = projectService.findByName(value.getName());
        if (project != null) {
            return Result.error("此名称已存在");
        }
        projectService.add(value);
        return Result.success();

    }
}
